package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.util.CookieUtil;

import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.KaptchaCodeRequest;
import com.cskaoyan.user.dto.KaptchaCodeResponse;
import com.cskaoyan.user.dto.UserRegisterRequest;
import com.cskaoyan.user.dto.UserRegisterResponse;
import com.cskaoyan.user.service.IAddressService;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.impl.UserRegisterImpl;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * /user
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class RegisterController {
    @Autowired
    UserRegisterImpl userRegister;
    @Autowired
    IKaptchaService iKaptchaService;
    @PostMapping("/register")
    public ResponseData userRegister(@RequestBody Map<String,String>map, HttpServletRequest request){

        String captcha = map.get("captcha");


        //验证码验证
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();

//        String userInfo = (String) request.getHeader("user_info");
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());

        String kaptch_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        //kaptcha_uuid=d401b4e2-ee8b-4802-a2d3-a5c40f12ced3
        kaptchaCodeRequest.setUuid(kaptch_uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String imageCode = kaptchaCodeResponse.getCode();
        if(!imageCode.equals(SysRetCodeConstants.SUCCESS.getCode())){

                    return ResponseData.msg(kaptchaCodeResponse.getMsg());
        }
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(map.get("email"));
        userRegisterRequest.setUserName(map.get("userName"));
        userRegisterRequest.setUserPwd(map.get("userPwd"));
        UserRegisterResponse userRegisterResponse= userRegister.userRegister(userRegisterRequest);
        String code = userRegisterResponse.getCode();
        if (code.equals(UserRetCode.SUCCESS.getCode())){
            return ResponseData.successNoResult();
        }


        return ResponseData.msg(userRegisterResponse.getMsg());

    }
}
